#include"StdAfx.h"


bool pgSylk::CreateFile(const char* sFilename)
{
	bool succes = pgFile::CreateFile(sFilename);

	if (succes)
	{

		PushFront("ID;P\nE");
	}

	return succes;
}

std::string pgSylk::GetValue(int row, int column)
{
	char buffer[MAX_BUFFER];
	sprintf(buffer, "C;X%d;Y%d;K", column, row);

	size_t pos = m_sTextBuffer.find(buffer);//FindString(buffer);

	if (pos == std::string::npos)
	{
		std::string notFound;
		notFound = "(NULL)";
		return notFound;
	}

	size_t end = m_sTextBuffer.find_first_of('C', pos + 1);
	
	std::string sValue;

	long length = (end - pos);
	long size	= length - strlen(buffer);

	char newBuffer[MAX_BUFFER];
	
	int read = m_sTextBuffer.copy(newBuffer, size, pos + strlen(buffer));

	newBuffer[read] = 0;
	
	sValue = newBuffer;

	sValue.erase(sValue.begin());

	size_t meh = sValue.find('"');
	sValue.erase(meh);

	return sValue;
}

bool pgSylk::SetValue(int row, int column, const char* value, ...)
{
	char argBuffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, value);
	vsprintf(argBuffer, value, pArgList);
	va_end (pArgList);

	char buffer[MAX_BUFFER];
	sprintf(buffer, "\nC;X%d;Y%d;K\"%s\"", column, row, argBuffer);
	
	std::string val = GetValue(row, column);
	if (val.compare("(NULL)") == 0)
	{
		// The entry doesn't exist
		size_t pos = m_sTextBuffer.find_last_of("E"); // just append to the end
		

		Insert(pos -1, buffer);
	}
	else
	{
		// TODO
		// Implement if value exists
		/*pos += sizeof(buffer);
		m_sTextBuffer.erase(pos, m_sTextBuffer.find_first_of('C', pos + 1));
		std::string FinalBuffer(argBuffer);
		FinalBuffer.insert(FinalBuffer.begin(), '"');
		FinalBuffer.insert(FinalBuffer.end(), '"');
		Insert(pos, "%s\n", FinalBuffer.c_str());*/
		size_t endPos = 0;
		size_t startPos = 0;
		std::string content;

		while (true)
		{
			startPos = m_sTextBuffer.find_first_of('\"', endPos +1);

			if (startPos == -1)
			{
				char buffer[MAX_BUFFER];
				sprintf(buffer, "ERROR: Failed to override value: %s in file: %s", value, m_sFilename.c_str());
				std::string message(buffer);
				g_PG_pLog->fTextoutTopic(RED, "pgSylk", message.c_str());
				return false;
			}

			endPos = m_sTextBuffer.find_first_of('\"', startPos + 1);

			if (endPos == -1)
			{
				char buffer[MAX_BUFFER];
				sprintf(buffer, "ERROR: Failed to override value: %s in file: %s", value, m_sFilename.c_str());
				std::string message(buffer);
				g_PG_pLog->fTextoutTopic(RED,"pgSylk", message.c_str() );
				return false;
			}

			content = m_sTextBuffer.substr(startPos + 1, endPos - startPos - 1);

			if (content.compare(val) == 0)
			{
				// Now check the x y coords if there are more than one entry with the same val
				int x, y;

				size_t posX = m_sTextBuffer.find_last_of('X', startPos) + 1;
				size_t posY = m_sTextBuffer.find_last_of('Y', startPos) + 1;

				size_t lenX = m_sTextBuffer.find_first_of(';', posX);
				size_t lenY = m_sTextBuffer.find_first_of(';', posY);

				std::string ystr = m_sTextBuffer.substr(posY, lenY - posY);
				std::string xstr = m_sTextBuffer.substr(posX, lenX - posX);

				y = atoi(ystr.c_str());
				x = atoi(xstr.c_str());

				if (x == row && y == column)
					break;
			}
		}

		ReplaceString(val.c_str(), value, startPos);

	}


	// TODO
	// Give error explanation if possible
	if (GetValue(row, column).compare(argBuffer))
		return true;
	else
		return false;
	
}

bool pgSylk::GetRowAndColumnOf(const char* sValue, int &row, int &column)
{
	//// Same as always
	//// This looks for " sValue " instead of "sValue"
	//int startPos = m_sTextBuffer.find_first_of(sValue);


	//if (startPos == -1)
	//{
	//	row = 0;
	//	column = 0;

	//	//g_PG_pLog->fTextoutTopic(RED, "[ERROR]: Could't find: " + sValue + "in Table: " + m_sFilename.c_str() "!\n", "pgSylk");
	//	return false;
	//}

	////int deklarationStart = m_sTextBuffer.find_last_of('C', startPos);

	//size_t x = m_sTextBuffer.find_last_of('X', startPos) + 1;
	//size_t xLen = m_sTextBuffer.find_first_of(';', x);

	//size_t y = m_sTextBuffer.find_first_of('Y', xLen) + 1;
	//size_t yLen = m_sTextBuffer.find_first_of(';', y);

	//std::string ystr = m_sTextBuffer.substr(y, yLen - y);
	//std::string xstr = m_sTextBuffer.substr(x, xLen - x);

	//row = atoi(ystr.c_str());
	//column = atoi(xstr.c_str());
	//

	// Other method

	size_t endPos = 0;
	std::string content;
	
	while (true)
	{
		size_t startPos = m_sTextBuffer.find_first_of('"', endPos +1);

		if (startPos == -1)
		{
			char buffer[MAX_BUFFER];
			sprintf(buffer, "ERROR: Couldn't find value: %s in file: %s", sValue, m_sFilename.c_str());
			std::string message(buffer);
			g_PG_pLog->fTextoutTopic(RED, "pgSylk", message.c_str());
			return false;
		}

		endPos = m_sTextBuffer.find_first_of('"', startPos + 1);

		if (endPos == -1)
		{
			char buffer[MAX_BUFFER];
			sprintf(buffer, "ERROR: Couldn't find value: %s in file: %s", sValue, m_sFilename.c_str());
			std::string message(buffer);
			g_PG_pLog->fTextoutTopic(RED, "pgSylk", message.c_str());
			return false;
		}

		content = m_sTextBuffer.substr(startPos + 1, endPos - startPos - 1);

		if (content.compare(sValue) == 0)
		{
			break;
		}
	}

	size_t x = m_sTextBuffer.find_last_of('X', endPos) + 1;
	size_t xLen = m_sTextBuffer.find_first_of(';', x);

	size_t y = m_sTextBuffer.find_first_of('Y', xLen) + 1;
	size_t yLen = m_sTextBuffer.find_first_of(';', y);

	std::string ystr = m_sTextBuffer.substr(y, yLen - y);
	std::string xstr = m_sTextBuffer.substr(x, xLen - x);

	row = atoi(ystr.c_str());
	column = atoi(xstr.c_str());
	

	return true;
}